import logging
from datetime import datetime

from flask import Blueprint, request
from flask import jsonify

from airflow.patsnap.service import resource_service
from airflow.models.idata_auth_resource import AuthResource
from airflow.www.api import exception_handle

log = logging.getLogger(__name__)

api_speech_resource = Blueprint('api_speech_resource', __name__)


@api_speech_resource.route('/resources/<int:rid>', methods=['GET'])
@exception_handle
def get_resource(rid):
    resource = AuthResource.get_resource(rid, None)
    return jsonify(code=0, message='ok', data=AuthResource.to_json(resource))


@api_speech_resource.route('/resources', methods=['GET'])
@exception_handle
def get_resources():
    page = request.args.get('page', 0, type=int)
    if page == 0:
        users = AuthResource.get_resources()
        map_user = map(AuthResource.to_json, users)
        return jsonify(code=0, message='ok', data=list(map_user))
    else:
        resource_result = AuthResource.get_resources_page(page)
        resource_result.items = list(map(AuthResource.to_json, resource_result.items))
        result = resource_result.to_json()
        return jsonify(code=0, message='ok', data=result)


@api_speech_resource.route('/resources', methods=['POST'])
@exception_handle
def add_resource():
    resource = request.json
    name = resource['name']
    url_prefix = resource['url_prefix']
    if name is None:
        log.error("add_resource error name=%s", name, )
        return jsonify(code=400, message="add_resource error name=None", data=None)
    if url_prefix is None:
        log.error("add_resource error url_prefix=%s", url_prefix, )
        return jsonify(code=400, message="add_resource error url_prefix=None", data=None)

    resource = AuthResource(name, url_prefix, datetime.now())
    AuthResource.add_resources(resource)
    return jsonify(code=0, message='ok', data=None)


@api_speech_resource.route('/resources/<int:id>', methods=['DELETE'])
@exception_handle
def delete_resource(id):
    resource_service.delete_resource(id)
    return jsonify(code=0, message='ok', data=None)


@api_speech_resource.route('/resources', methods=['PUT'])
@exception_handle
def update_resource():
    resource = request.json
    resource_id = resource['id']
    name = resource['name']
    url_prefix = resource['url_prefix']

    if id is None:
        log.error("update_resource error id=%s", id, )
        return jsonify(code=400, message="add_resource error id=None", data=None)
    if name is None:
        log.error("update_resource error name=%s", name, )
        return jsonify(code=400, message="add_resource error name=None", data=None)
    if url_prefix is None:
        log.error("update_resource error url_prefix=%s", url_prefix, )
        return jsonify(code=400, message="add_resource error url_prefix=None", data=None)

    resource = AuthResource(name, url_prefix, datetime.now())
    resource.id = resource_id
    AuthResource.update_resource(resource)
    return jsonify(code=0, message='ok', data=None)
